package com.net.xpay.common.manager.impl.sync;

import com.google.common.base.Preconditions;
import com.net.xpay.common.manager.sync.CommonChargeRecordManager;
import com.net.common.annotation.ReadTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractManager;
import com.net.xpay.common.dao.sync.CommonChargeRecordMapper;
import com.net.xpay.common.domain.sync.CommonChargeRecord;
import com.net.xpay.core.model.ChargeRecordSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;


@Component
@ReadTransactional
public class CommonChargeRecordManagerImpl extends AbstractManager<CommonChargeRecord> implements CommonChargeRecordManager {

    @Autowired
    private CommonChargeRecordMapper mapper;


    @Override
    protected BasicMapper<Long, CommonChargeRecord> getMapper() {
        return mapper;
    }

    @Override
    public int save(CommonChargeRecord obj) {
        return getMapper().insert(obj);
    }

    @Override
    public int update(CommonChargeRecord obj) {
        Preconditions.checkArgument(obj != null && obj.getId() != null);
        return getMapper().updateByPrimaryKey(obj);
    }

    @Override
    public List<CommonChargeRecord> search(ChargeRecordSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(ChargeRecordSearchModel searchModel) {
        return mapper.searchCount(searchModel);
    }

    @Override
    public long sumSuccessAmount(Long userId, LocalDateTime endMinTime,LocalDateTime endMaxTime) {

        return mapper.sumSuccessAmount(userId,endMinTime,endMaxTime);
    }

    @Override
    public List<CommonChargeRecord> list(ChargeRecordSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }
}
